﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;


namespace GiaoDien
{
    public partial class FormTraSach : Form
    {
        private DataTable docgia;
        private DataTable showdata;
        public FormTraSach()
        {
            InitializeComponent();
        }

        private void loadngaytrasach()
        {
            tbngaytra.Text = DateTime.Now.ToShortDateString();
        }

        private void init()
        {
            docgia = new System.Data.DataTable();
            showdata = new DataTable();
        }

        private void loaddocgia()
        {
            DOCGIABUS dgBUS = new DOCGIABUS();
            docgia = dgBUS.Loaddocgia();
        }

        private void Loadmadocgia()
        {
            if (docgia.Rows.Count != 0)
            {
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                tbmadocgia.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                tbmadocgia.AutoCompleteSource = AutoCompleteSource.CustomSource;
                IDataReader read = docgia.CreateDataReader();
                while (read.Read())
                {
                    auto.Add(read["MaDocGia"].ToString());
                }
                tbmadocgia.AutoCompleteCustomSource = auto;
            }
        }

        private void FormTraSach_Load(object sender, EventArgs e)
        {
            loadngaytrasach();
            loaddocgia();
            Loadmadocgia();
            taoshowdata();
            showdata = taoshowdata();
        }

        private void tbmadocgia_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string madocgia = tbmadocgia.Text.Trim().ToUpper();
                if (madocgia != "")
                {
                    PHIEUMUONSACHBUS pmBUS = new PHIEUMUONSACHBUS();
                    PHIEUMUONSACHDTO pmDTO = new PHIEUMUONSACHDTO();
                    pmDTO = pmBUS.layphieumuonsach(madocgia);

                    DOCGIABUS dgBUS = new DOCGIABUS();
                    DataTable datatable = dgBUS.Loaddocgia(pmDTO.Madocgia);
                    DataRow row = datatable.Rows[0];
                    tbtendocgia.Text = row["HoTen"].ToString();
                    tbngaysinh.Text = row["NgaySinh"].ToString();
                    tbemail.Text = row["Email"].ToString();
                    tbdiachi.Text = row["DiaChi"].ToString();
                    tbdienthoai.Text = row["DienThoai"].ToString();
                    tbngaylapthe.Text = row["NgayLapThe"].ToString();
                    tbngayhethan.Text = row["NgayHetHan"].ToString();
                    tbloaidocgia.Text = row["MaLoaiDocGia"].ToString();
                    tbtienno.Text = row["TienNo"].ToString();


                    CHITIETPHIEUMUONSACHBUS ctpmBUS = new CHITIETPHIEUMUONSACHBUS();
                    DataTable ctpm = new System.Data.DataTable();
                    ctpm = ctpmBUS.laychitietphieumuonsach(pmDTO.Maphieumuonsach.ToString());
                    DataRow row2;
                    DateTime now = DateTime.Now;
                    DateTime day;
                    int distance;
                    TimeSpan time;
                    QUYDINHSACHBUS qdsBUS = new QUYDINHSACHBUS();
                    foreach (DataRow row1 in ctpm.Rows)
                    {
                        row2 = showdata.NewRow();
                        row2["MaSach"] = row1["MaSach"].ToString();

                        row2["TenSach"] = row1["TenSach"].ToString();
                        row2["NgayMuon"] = pmDTO.Ngaymuon.ToShortDateString();
                        day = Convert.ToDateTime(pmDTO.Ngaymuon.ToShortDateString());
                        time = now - day;
                        distance = time.Days;
                        if (distance > 3)
                            distance = distance - 3;
                        else if (distance < 3)
                            distance = 0;
                        row2["SoNgayMuon"] = distance.ToString();
                        int kq = qdsBUS.tinhtienphat(distance);
                        row2["TienPhat"] = kq.ToString();
                        showdata.Rows.Add(row2);
                    }
                    this.dataGridView1.DataSource = showdata;
                }

            }
        }

        private DataTable taoshowdata()
        {
            DataTable temp = new DataTable();

            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.AllowDBNull = false;
            column.Caption = "MÃ SACH";
            column.ColumnName = "MaSach";
            temp.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.AllowDBNull = false;
            column.Caption = "TÊN SÁCH";
            column.ColumnName = "TenSach";
            temp.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.AllowDBNull = false;
            column.Caption = "NGÀY MƯỢN";
            column.ColumnName = "NgayMuon";
            temp.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.AllowDBNull = false;
            column.Caption = "SỐ NGÀY MƯỢN";
            column.ColumnName = "SoNgayMuon";
            temp.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.AllowDBNull = false;
            column.Caption = "TIỀN PHẠT";
            column.ColumnName = "TienPhat";
            temp.Columns.Add(column);

            return temp;
        }

        private void btnlapthe_Click(object sender, EventArgs e)
        {

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void reset()
        {
            tbmadocgia.ResetText();
            tbtendocgia.ResetText();
            tbngaysinh.ResetText();
            tbemail.ResetText();
            tbdiachi.ResetText();
            tbdienthoai.ResetText();
            tbngaylapthe.ResetText();
            tbngayhethan.ResetText();
            tbloaidocgia.ResetText();
            tbtienphat.ResetText();
            tbtienno.ResetText();
            tbtongno.ResetText();
            showdata.Clear();
            this.dataGridView1.DataSource = showdata;
        }

        private void btnthemoi_Click(object sender, EventArgs e)
        {
            reset();
        }

        private void btntinhtien_Click(object sender, EventArgs e)
        {
            double tong = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                {
                    tong += double.Parse(dataGridView1.Rows[i].Cells["TienPhat"].Value.ToString());
                }
            }
            tbtienphat.Text = tong.ToString();
            tong += Convert.ToDouble(tbtienno.Text);
            tbtongno.Text = tong.ToString();
        }



    }
}
